# coding: utf-8

from flask_wtf import FlaskForm
from wtforms.validators import DataRequired, Optional
from wtforms import (
    StringField, IntegerField
)
from wtforms_components import read_only

from app.forms import NonValidatingSelectField
from app.products.constant import TypeToName


class BrandForm(FlaskForm):
    product_type_id = NonValidatingSelectField(
        '产品类型',
        coerce=int,
        choices=list(TypeToName.mapping.items()),
        validators=[DataRequired()]
    )
    brand_name = StringField('品牌名称', validators=[DataRequired()])
    weight = IntegerField('权重', default=0)

    def __init__(self, *args, is_read_only=False, **kwargs):
        super(BrandForm, self).__init__(*args, **kwargs)
        if is_read_only:
            read_only(self.product_type_id)


class BrandSearchForm(FlaskForm):
    """
    品牌查询条件FORM
    """
    product_type_id = NonValidatingSelectField(
        '产品类型',
        coerce=int,
        choices=list(),
        validators=[Optional()]
    )
    brand_name = StringField('品牌名称', validators=[Optional()])

    def __init__(self, *args, **kwargs):
        super(BrandSearchForm, self).__init__(*args, **kwargs)
        self.product_type_id.choices = (
            [(0, '--请选择产品类型--')] + list(TypeToName.mapping.items())
        )


class SpecSearchForm(FlaskForm):
    """
    规格查询条件表单
    """
    product_type_id = NonValidatingSelectField(
        '产品类型',
        coerce=int,
        choices=list(),
        validators=[Optional()]
    )
    name = StringField('规格名称', validators=[Optional()])

    def __init__(self, *args, **kwargs):
        super(SpecSearchForm, self).__init__(*args, **kwargs)
        self.product_type_id.choices = (
            [(0, '--请选择产品类型--')] + list(TypeToName.mapping.items())
        )